package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.检查参数

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数错误");
        }

        dto.checkParam();

        //2.查询

        // 2.1 封装分页条件

        Page queryPage = new Page(dto.getPage(), dto.getSize());

        // 2.2 查询条件
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();
        if (StringUtils.isNotEmpty(dto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }

        IPage<AdSensitive> pageResult = page(queryPage, queryWrapper);


        //3.封装结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());


        //4.返回
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.检查参数

        if (adSensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数错误");
        }

        adSensitive.setCreatedTime(new Date());

        save(adSensitive);
        //2.保存

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {

        //1.检查参数
        if (adSensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "给了个屁，让我查成分吗");
        }
        //2.查询是否存在
//        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();
//        queryWrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives());
//        AdSensitive one = this.getOne(queryWrapper);
        updateById(adSensitive);
        //3.更新 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {

        //1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "不存在的ID奥");
        }
            //2.查询敏感词是否存在

        AdSensitive adSensitive = getById(id);

        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "数据库里的这个ID估计是个鬼ID");
        }


        //3.执行删除操作

        removeById(id);


            //4.返回结果

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
